package com.dao;

import com.pojo.Classs;
import com.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @CreateTime: 2022-12-24  16:03
 * @Description: TODO
 * @Version: 1.0.0
 */
public class IClasssDAOImpl implements IClasssDAO {

    private static Connection connection;

    static {
        try {
            // 拿到连接
            connection = JdbcUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Classs> findAllList() {
        List<Classs> classses;
        try {
            PreparedStatement preparedStatement = connection.prepareStatement("select * from tb_classs");
            ResultSet resultSet = preparedStatement.executeQuery();
            classses = new ArrayList<>();
            while (resultSet.next()) {
                Classs classs = new Classs(resultSet.getInt("id"), resultSet.getString("class_name"),resultSet.getString("class_grade"));
                classses.add(classs);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return classses;
    }

    public static void main(String[] args) {
        IClasssDAOImpl iClasssDAO = new IClasssDAOImpl();
        List<Classs> allList = iClasssDAO.findAllList();
        System.out.println(allList);
    }


}
